#include "StdAfx.h"
#include "Node.h"
#include "SCheduleManager.h"
#include "Parameter.h"
CFormativeTrace CNode::s_cFormatTracer("result\\PeerLog.log");
void CNode::Log(int nLevel,const char *pFormat,...)
{
	va_list marker;
	va_start( marker, pFormat );
	s_cFormatTracer.Log(TRACE_TO_FILE|TRACE_TO_DEBUGER,g_dwCurrentTime,this->GetNodeIndex(),nLevel,pFormat,marker);
	va_end( marker );
}
CNode::CNode(const int nNodeIndex)
:m_nNodeIndex(nNodeIndex)
{
}

CNode::~CNode(void)
{
}

bool CNode::AddTimer(const TIMER_TYPE dwType, const DWORD dwDelay, const DWORD dwPeriod)
{
	if(m_cTimerSet.insert(dwType).second==true)
	{
		return g_pScheduler->AddTimer(dwType,dwDelay+g_dwCurrentTime,dwPeriod,this);
	}
	else
	{
		return false;
	}
}
void CNode::OnTimer(const TIMER_TYPE dwType)
{
	if(dwType == TM_JOIN)
	{
		this->OnJoin();
	}
	if(dwType == TM_LEAVE)
	{
		this->OnLeave();
	}
	return ;
}
void CNode::OnJoin(void)
{
	Log(LEVEL_INFO,"Node %d Join.\n",(int)m_nNodeIndex);
	this->m_nJoinTime = g_dwCurrentTime;
}

void CNode::OnLeave(void)
{
	Log(LEVEL_INFO,"Node %d Leave.\n",(int)m_nNodeIndex);
	this->m_nLeaveTime = g_dwCurrentTime;
}

bool CNode::AddAlarm(TIMER_TYPE dwType, DWORD dwDelay)
{
	if(true == m_cTimerSet.insert(dwType).second)
	{
		return g_pScheduler->AddTimer(dwType,dwDelay+g_dwCurrentTime,0,this);
	}
	else
	{
		return false;
	}
};

bool CNode::KillTimer(TIMER_TYPE dwType)
{
	m_cTimerSet.erase(dwType);
	return g_pScheduler->KillTimer(dwType,this);
}


